package Model;

import java.io.Serializable;

import Messaging.Notification;
import Messaging.NotificationManager;

/*
 * @author Roch San Buenaventura
 */
public class UtilitySpace extends PropertySpace implements Serializable
{
	
	final static long serialVersionUID = 232;
	
	public UtilitySpace()
	{
		Name_ = "NONE";
		purchasePrice_ = 0;
		mortgageValue_ = 0;
		State_ = SpaceUnowned.Instance();
		Owner_ = null;
	}
	
	public UtilitySpace(String name, int type, int spaceNumber, int purchasePrice, int mortgageValue)
	{
		Name_ = name;
		Type_ = type;
		SpaceNumber_ = spaceNumber;
		purchasePrice_ = purchasePrice;
		mortgageValue_ = mortgageValue;
		State_ = SpaceUnowned.Instance();
		Owner_ = null;
	}
	
	public String toString() 
	{
		String string = "Property Name: " + Name_;
		if (Owner_ == null)
			string += ", Owned by: Nobody";
		else
			string += ", Owned by: " + Owner_.getName();
		
		return string;
	}
	
	public void changeState(SpaceState newState) 
	{
		State_ = newState;
	}
	
	public void landOn(Player p) 
	{
		State_.landOn(p, this);
	}

	public void setOwner(Player p) 
	{
		Owner_ = p;
		NotificationManager.getInstance().notifyObservers(Notification.CHANGED_OWNER, this);
	}
	
	public Player getOwner() 
	{
		return Owner_;
	}
	
	public void setPurchasePrice(int purchasePrice)
	{
		purchasePrice_ = purchasePrice;
	}

	public int getPurchasePrice()
	{
		return purchasePrice_;
	}

	public void setMortgageValue(int mortgageValue)
	{
		mortgageValue_ = mortgageValue;
	}

	public int getMortgageValue()
	{
		return mortgageValue_;
	}
	
	public void bePurchased(Player owner)
	{
		setOwner(owner);
		
		SpaceState newState = owner.updateTypeIncrease(Type_);
		
		if (newState != null)
			State_ = newState;
		
		else
			
			State_ = UtilityOwns1.Instance();
	}
	
	public void beMortgaged()
	{
		State_.changeState(this, SpaceMortgaged.Instance());
		Owner_.updateTypeDecrease(Type_);
	}
	
	public void unmortgage()
	{
		SpaceState newState = Owner_.updateTypeIncrease(Type_);
		
		if (newState != null)
			State_ = newState;
		
		else
			State_ = UtilityOwns1.Instance();
	}
	
	public SpaceState getState() 
	{
		return State_;
	}
	
	public int getMultiplier() 
	{
		return State_.getMultiplier();
	}
}
